home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / packet / p_aa4re / bb212src / bbsrtq.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1991-03-12  |  8.5 KB  |  240 lines

  1. (*===========================================================================*)
  2. (* Procedures to handle TNC queueing                                         *)
  3. (*                                                                           *)
  4. (*   Copyright 1988, 1989, 1991 by H. Roy Engehausen.  All rights reserved.  *)
  5. (*   This software may be freely distributed and used, but it may not        *)
  6. (*   under any circumstances be sold by anyone other than the author.        *)
  7. (*   It may be distributed by a commercial company as long as it is          *)
  8. (*   for no cost.                                                            *)
  9. (*                                                                           *)
  10. (*===========================================================================*)
  11.  
  12. (*===========================================================================*)
  13. (* TNC_DATA_QUEUED                                                           *)
  14. (*   Returns true if data in queue.  Will also pop the oldest element off    *)
  15. (*   queue and put it in the buffer.                                         *)
  16. (*===========================================================================*)
  17.  
  18. FUNCTION tnc_data_queued : BOOLEAN;
  19.  
  20.   VAR
  21.     b           : BOOLEAN;
  22.     i           : WORD;
  23.     last_chain  : str_m_chain;
  24.     look_chain  : str_m_chain;
  25.  
  26.   BEGIN;
  27.  
  28.     WITH active_tcb^, active_tcb^.tnc_data DO
  29.       BEGIN;
  30.  
  31.         {$IFDEF DEBUG}
  32.           WRITELN('chain check! -- ', channel, ' -- ', tnc_cmd_data);
  33.         {$ENDIF}
  34.  
  35.         (*-------------------------------------------------------------------*)
  36.         (* Get ready to chase down the chain                                 *)
  37.         (*-------------------------------------------------------------------*)
  38.  
  39.         look_chain := tnc_in_chn;
  40.         b := FALSE;
  41.  
  42.         REPEAT
  43.  
  44.           {$IFDEF DEBUG}
  45.             WITH look_chain^ DO
  46.               WRITELN(str_m_chan, ' -- ', str_m_type);
  47.           {$ENDIF}
  48.  
  49.           {$IFDEF POINT_CHK}
  50.             test_pointer(look_chain);
  51.           {$ENDIF}
  52.  
  53.           (*-----------------------------------------------------------------*)
  54.           (* See if we have a match on what's wanted and what we have        *)
  55.           (*-----------------------------------------------------------------*)
  56.  
  57.           WITH look_chain^ DO
  58.             IF (channel = str_m_chan)
  59.                AND ((tnc_cmd_data = 2) OR
  60.                     ((tnc_cmd_data = 3) AND (str_m_type >= t_to_h_mh_noi)) OR
  61.                     ((tnc_cmd_data = 4) AND (str_m_type < t_to_h_mh_noi)))
  62.                     THEN
  63.               b := TRUE
  64.             ELSE
  65.               BEGIN;
  66.                 last_chain := look_chain;
  67.                 look_chain := str_m_next;
  68.               END;
  69.  
  70.           (*-----------------------------------------------------------------*)
  71.           (* Loop until we find something or run out of places to look       *)
  72.           (*-----------------------------------------------------------------*)
  73.  
  74.         UNTIL b OR (look_chain = NIL);
  75.  
  76.         (*-------------------------------------------------------------------*)
  77.         (* If not lucky, leave.                                              *)
  78.         (*-------------------------------------------------------------------*)
  79.  
  80.         IF NOT b THEN
  81.           BEGIN;
  82.             tnc_data_queued := FALSE;
  83.             EXIT;
  84.           END;
  85.  
  86.         (*-------------------------------------------------------------------*)
  87.         (* Move data from FIFO queue to buffer                               *)
  88.         (*-------------------------------------------------------------------*)
  89.  
  90.         WITH look_chain^ DO
  91.           BEGIN;
  92.  
  93.             {$IFDEF POINT_CHK}
  94.               test_pointer(look_chain);
  95.             {$ENDIF}
  96.  
  97.             (*---------------------------------------------------------------*)
  98.             (* Move data, type, and set null switch                          *)
  99.             (*---------------------------------------------------------------*)
  100.  
  101.             MOVE(str_m_data, tnc_data, str_m_data.long_length + 3);
  102.             tnc_type := str_m_type;
  103.             tnc_null := tnc_data.long_length = 0;
  104.  
  105.             (*---------------------------------------------------------------*)
  106.             (* Remove buffer from chain                                      *)
  107.             (*---------------------------------------------------------------*)
  108.  
  109.             IF look_chain = tnc_in_chn THEN
  110.               BEGIN;
  111.                 tnc_in_chn := str_m_next;
  112.                 {$IFDEF DEBUG_SRTQ}
  113.                   last_chain := NIL;
  114.                 {$ENDIF}
  115.               END
  116.             ELSE
  117.               last_chain^.str_m_next := str_m_next;
  118.  
  119.             (*---------------------------------------------------------------*)
  120.             (* Size of buffer                                                *)
  121.             (*---------------------------------------------------------------*)
  122.  
  123.             i := str_m_data.long_length + 3 + 6;
  124.  
  125.             {$IFDEF DEBUG_SRTQ}
  126.               trace_data('SRTQFR', i, look_chain, '');
  127.               trace_data('SRTQFC', i, last_chain, '');
  128.               trace_data('SRTQFN', i, str_m_next, '');
  129.             {$ENDIF}
  130.  
  131.           END;
  132.  
  133.       END;
  134.  
  135.     (*-----------------------------------------------------------------------*)
  136.     (* Dispose of buffer                                                     *)
  137.     (*-----------------------------------------------------------------------*)
  138.  
  139.     {$IFDEF DEBUG}
  140.       WRITELN('Freeing = ', i);
  141.     {$ENDIF}
  142.  
  143.     FREEMEM(look_chain, i);
  144.  
  145.     {$IFDEF FREE_CHK}
  146.       test_free_list;
  147.     {$ENDIF}
  148.  
  149.     (*-----------------------------------------------------------------------*)
  150.     (* Signal that we did it                                                 *)
  151.     (*-----------------------------------------------------------------------*)
  152.  
  153.     tnc_data_queued := TRUE;
  154.  
  155.   END;
  156.  
  157. (*===========================================================================*)
  158. (* ADD_TNC_QUEUE                                                             *)
  159. (*      Adds data to a thread's queue                                        *)
  160. (*===========================================================================*)
  161.  
  162. PROCEDURE add_tnc_queue(this_tcb     : tcb_ptr;
  163.                         this_channel : BYTE);
  164.   VAR
  165.     i           : WORD;
  166.     last_chain  : str_m_chain;
  167.     look_chain  : str_m_chain;
  168.  
  169.   BEGIN;
  170.  
  171.     WITH active_tcb^.tnc_data DO
  172.  
  173.     (*-----------------------------------------------------------------------*)
  174.     (* Get a buffer space                                                    *)
  175.     (*-----------------------------------------------------------------------*)
  176.  
  177.     i := long_length + 3 + 6;
  178.  
  179.     GETMEM(last_chain, i);
  180.  
  181.     (*-----------------------------------------------------------------------*)
  182.     (* Build a buffer                                                        *)
  183.     (*-----------------------------------------------------------------------*)
  184.  
  185.     WITH last_chain^ DO
  186.       BEGIN;
  187.         str_m_next := NIL;
  188.         str_m_type := active_tcb^.tnc_type;
  189.         str_m_chan := this_channel;
  190.         MOVE(active_tcb^.tnc_data, str_m_data,
  191.                                          active_tcb^.tnc_data.long_length + 3);
  192.       END;
  193.  
  194.     (*-----------------------------------------------------------------------*)
  195.     (* Chain it on "FIFO"                                                    *)
  196.     (*-----------------------------------------------------------------------*)
  197.  
  198.     WITH this_tcb^ DO
  199.       BEGIN;
  200.         look_chain := tnc_in_chn;
  201.         IF look_chain = NIL THEN
  202.           BEGIN;
  203.  
  204.             {$IFDEF DEBUG_SRTQ}
  205.               trace_data('SRTQAH', i, last_chain, '');
  206.             {$ENDIF}
  207.  
  208.             tnc_in_chn := last_chain
  209.  
  210.           END
  211.         ELSE
  212.           BEGIN;
  213.  
  214.             WHILE look_chain^.str_m_next <> NIL DO
  215.               BEGIN;
  216.  
  217.                 {$IFDEF POINT_CHK}
  218.                   test_pointer(look_chain);
  219.                 {$ENDIF}
  220.  
  221.                 look_chain := look_chain^.str_m_next;
  222.               END;
  223.  
  224.             {$IFDEF POINT_CHK}
  225.               test_pointer(look_chain);
  226.             {$ENDIF}
  227.  
  228.             look_chain^.str_m_next := last_chain;
  229.  
  230.             {$IFDEF DEBUG_SRTQ}
  231.               trace_data('SRTQAE', i, last_chain, '');
  232.               trace_data('SRTQAP', 0, look_chain, '');
  233.             {$ENDIF}
  234.  
  235.           END;
  236.  
  237.       END;
  238.  
  239.   END;
  240.